<template>
  <el-dialog v-if="load" title="生成发货计划" v-model="visible" append-to-body :close-on-click-modal="false" width="500px"
    :z-index="2000" @close="handleClose">
    <div class="h-20 flex items-center">
      <VSelect v-model="form.plan_destination_warehouse" placeholder="目的仓" :options="options.plan_destination_warehouse"
       filterable clearable class="w-full" />
    </div>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { resSkip, resMessage } from "@/utils/res";

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const demand_type = ref('')
const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (pds_ids,type) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { pds_ids, is_skip: 1,list:[{}] }
  loadings = reactive({})
  demand_type.value = type
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = (is_skip = 1) => {
  loadings.submit = true
  const closeLoading = () => loadings.submit = false
  api.plan({ ...form.value, is_skip }).then(async res => {
    const { message, data } = res
    if (message) {
      if (data.is_skip !== 2) {
        ElMessage.error(message)
        return
      } else {
        resSkip({
          res,
          closeLoading,
          success: () => {
            emit('success')
            close()
          },
          submit: () => {
            submit(2)
          },
        })
      }
    } else {
      resMessage({
        res,
        success: () => {
          ElMessage.success('操作成功')
          close()
        }
      })
    }
  }).finally(closeLoading)
}

const handleAdd = ()=>{
  form.value.list.push({})
}

defineExpose({ open })
</script>